import {
  mapGetters
} from 'vuex';
const json = require('@/common/json/graph.json');
const TransStepBaseMixin = {
  props: {
    dialog: {
      type: Boolean,
      required: false,
      defalut: false
    },
    dialogName: {
      type: String,
      required: false,
      defalut: ""
    },
    attributes: {
      type: NamedNodeMap,
      required: true
    },
    getDatabaseNames: {
      type: Function,
      default: null,
    },
    getExportModelXML: {
      type: Function,
      default: null,
    },
    getPreviousSteps: {
      type: Function,
      default: null,
    },
    getAfterSteps: {
      type: Function,
      default: null,
    },
  },
  data() {
    return {
      calculatorType: json.calculatorType,
      isSureType: json.isSureType,
      dataType: json.dataType,
      formatNum: json.formatNum,
      formatDate: json.formatDate,
      compareType:json.compareType,
      trimType:json.trimType,
      randomType:json.randomType,
      isSureType:json.isSureType
    }
  },
  computed: {
    ...mapGetters(['sidebar']),
    isHeight() {
      return this.sidebar.height;
    }
  },
  methods: {
    closeDialog() {
      this.$emit("close-dialog", this.dialog);
    },
    selectChange(prop, value, row) {
      if (prop === "type") {
        row.format = "";
        this.changeFormat(prop, value, row);
      }
    },
    changeFormat(prop, value, row) {
      let arr = [];
      if (value === "1" || value === "5" || value === "6") {
        arr = arr.concat(this.formatNum);
      } else if (value === "3") {
        arr = arr.concat(this.formatDate);
      } else if (value === "2") {
        arr = this.formatNum.concat(this.formatDate);
      }
      for (let i = 0; i < this.parametersData.length; i++) {
        let prop_ = this.parametersData[i].prop;
        let attributes = this.parametersData[i].attributes;
        if (prop_ === "format") {
          attributes.selectData = arr;
        }
      }
    },
    isJsonString(str) {
      try {
        if (typeof JSON.parse(str) === "object") {
          return true;
        }
      } catch (e) {}
      return false;
    },
    getAttributes() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let attributes = JSON.parse(JSON.stringify(this.form));
          this.$emit("getAttributes", attributes);
        }
      });
    },
    initForm() {
      Object.values(this.attributes).forEach((value, index) => {
        if (this.form.hasOwnProperty(value.name)) {
          if (this.isJsonString(value.value)) {
            this.form[value.name] = JSON.parse(value.value);
          } else {
            this.form[value.name] = value.value;
          }
        }
      });
    }
  },
  created() {
    this.initForm();
  },
  mounted() {},
  beforeDestroy() {},
};

export default TransStepBaseMixin
